﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using CFE;
using CFEApp.Utility;

namespace CFEApp.Controllers
{
	class ProjectController : Singleton<ProjectController>
	{

		private Project currentProject;
		private Dictionary<Project.WorkItem, ClosureProxy> proxies;

		public ProjectController()
		{
			proxies = new Dictionary<Project.WorkItem, ClosureProxy>();
		}


		public Project OpenProject(string filename)
		{

			currentProject = ProjectReader.ReadFile(filename);

			proxies.Clear();

			foreach (var item in currentProject.Items)
			{
				var proxy = new ClosureProxy(item);
				proxy.CompilationComplete += proxy_CompilationComplete;
				proxies.Add(item, proxy);
			}

			return currentProject;
		}

		public Project GetCurrentProject()
		{
			return currentProject;
		}


		internal void Build()
		{
			foreach (var proxy in proxies.Values)
			{
				proxy.Start();
			}
		}

		void proxy_CompilationComplete(object sender, ClosureProxy.CompilationCompleteEventArgs e)
		{
			if (CompilationComplete != null)
				CompilationComplete(this, e);
		}

		public event EventHandler<CFE.ClosureProxy.CompilationCompleteEventArgs> CompilationComplete;
	}
}
